﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;

namespace Lamex.mMachine.UI
{
    public partial class frmMAC_MT_Outsource : frmBaseChild
    {
        string strSql;
        DataTable dt;
        string IsEdit = "";
        DataTable table_Maintain;
        public frmMAC_MT_Outsource()
        {
            InitializeComponent();
        }

        private void frmMAC_MT_Outsource_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width - 330;
        }

        private void frmMAC_MT_Outsource_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            gridView1.OptionsBehavior.Editable = false;
            //文字左对齐，直接在设计器中设置，以下无效
            //vGridControl1.Appearance.RecordValue.HAlignment = DevExpress.Utils.HorzAlignment.Near;
            vGridControl1.OptionsBehavior.Editable = false;
            setvGridControlRepository();
            setGridControlData();

        }

        //设置控件可选项
        private void setvGridControlRepository()
        {
            try
            {
                table_Maintain = Common.GetDataTable.GetTable_Outsource();
                DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit searchLookUpEdit = new DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit();
                searchLookUpEdit.NullText = string.Empty;
                searchLookUpEdit.DataSource = table_Maintain;
                searchLookUpEdit.ValueMember = "MAINTAIN_NO";
                searchLookUpEdit.DisplayMember = "MAINTAIN_NO";
                vGridControl1.Rows["rowMAINTAIN_NO"].Properties.RowEdit = searchLookUpEdit;
                searchLookUpEdit.EditValueChanged += new System.EventHandler(searchLookUpEdit_EditValueChanged);

                DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
                vGridControl1.Rows["rowWORK_CONTENT"].Properties.RowEdit = memoEdit;

                //DevExpress.XtraEditors.Repository.RepositoryItemDateEdit dateEdit = new DevExpress.XtraEditors.Repository.RepositoryItemDateEdit();
                //vGridControl1.Rows["OUTSOURCE_DATE"].Properties.RowEdit = dateEdit;
                //vGridControl1.Rows["RETURN_DATE"].Properties.RowEdit = dateEdit;

                DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_state = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                comboBox_state.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
                string[] typeList = new string[] { "已完成", "取消" };
                foreach (var item in typeList)
                {
                    comboBox_state.Items.Add(item);
                }
                vGridControl1.Rows["rowISSUE_STATE"].Properties.RowEdit = comboBox_state;
                //加载选项“已完成,取消"，不允许用户后期增加选项，所以静态加载
                DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_outstate = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                comboBox_outstate.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
                string[] typeOutList = new string[] { "已停机", "未停机" };
                foreach (var item in typeOutList)
                {
                    comboBox_outstate.Items.Add(item);
                }
                vGridControl1.Rows["rowOUTSOURCE_STATE"].Properties.RowEdit = comboBox_outstate;


            }
            catch (Exception ex)
            {
            }
        }
        private void searchLookUpEdit_EditValueChanged(object sender, EventArgs e)
        {
            string strMAINTAIN_NO = vGridControl1.EditingValue.ToString();
            DataTable dataTable = table_Maintain.Select("MAINTAIN_NO='" + strMAINTAIN_NO + "'").CopyToDataTable();
            vGridControl1.Rows["rowMACHINE_NO"].Properties.Value = dataTable.Rows[0]["MACHINE_NO"].ToString();
            vGridControl1.Rows["rowMACHINE_DESC"].Properties.Value = dataTable.Rows[0]["MACHINE_DESC"].ToString();
            vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value = strMAINTAIN_NO;
        }
        //加载所有委外维修清单
        private void setGridControlData()
        {
            strSql = @"SELECT OUTSOURCE_NO,
c.MAINTAIN_NO,
c.MACHINE_NO,
MACHINE_DESC,
OUTSOURCE_FACTORY,
OUTSOURCE_STATE,
WORK_CONTENT,
OUTSOURCE_DATE,
RETURN_DATE,
MAINTAIN_COST,
ISSUE_STATE,
PROCESS_PLAN,
c.UPDATE_TIME,
c.UPDATE_USER
FROM
(SELECT OUTSOURCE_NO,a.MAINTAIN_NO,MACHINE_NO,PROCESS_PLAN,OUTSOURCE_FACTORY,OUTSOURCE_STATE,
WORK_CONTENT,OUTSOURCE_DATE,RETURN_DATE,MAINTAIN_COST,ISSUE_STATE,b.UPDATE_TIME,b.UPDATE_USER
FROM [LamexMES_FAMA].[dbo].[MT_Maintain_Record] AS a
LEFT JOIN 
[LamexMES_FAMA].[dbo].[MT_Outsource_Record] AS b
ON a.MAINTAIN_NO=b.MAINTAIN_NO) AS c
LEFT JOIN 
[LamexMESManager].[dbo].[FAMA_Account_Basic_Info] AS d
ON c.MACHINE_NO=d.MACHINE_NO
WHERE PROCESS_PLAN='委外维修' ORDER BY c.UPDATE_TIME ";

            gridControl1.DataSource = null;
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            gridControl1.DataSource = dt;

            //遍历并设置自动列宽
            setGridViewColumnAutoWidth();

            vGridControl1.DataSource = null;
        }

        private void setGridViewColumnAutoWidth()
        {
            gridView1.OptionsView.ColumnAutoWidth = false;
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.BestFitColumns();
                if (i == 2)
                {
                    gridView1.Columns[2].Width = 20;
                }
                else
                {
                    gridView1.Columns[i].BestFit();//自动列宽    
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("1、保存[右侧编辑区]当前数据至[LamexMES_FAMA].[dbo].[MT_Outsource_Record]；" +
            //    "\r\n" + "2、保存前检查用户是否有数据修改权限");
            ////数据修改权限逻辑：检查当前用户是否有表修改权限，通过[LamexMESManager].[dbo].[Sys_DataAuthority]比对
            //vGridControl1.DataSource = null;
            try
            {
                //if (!Common.CRUDHelper.IsExistList("MT_Outsource_Record", "LamexMES_FAMA")) return;
                string strMAINTAIN_NO = vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value!=null?
                    vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value.ToString():"";
                string strMACHINE_NO = vGridControl1.Rows["rowMACHINE_NO"].Properties.Value!=null?
                    vGridControl1.Rows["rowMACHINE_NO"].Properties.Value.ToString():"";
                string strMACHINE_DESC = vGridControl1.Rows["rowMACHINE_DESC"].Properties.Value!=null?
                    vGridControl1.Rows["rowMACHINE_DESC"].Properties.Value.ToString():"";
                string strOUTSOURCE_FACTORY = vGridControl1.Rows["rowOUTSOURCE_FACTORY"].Properties.Value!=null?
                    vGridControl1.Rows["rowOUTSOURCE_FACTORY"].Properties.Value.ToString():"";
                string strWORK_CONTENT = vGridControl1.Rows["rowWORK_CONTENT"].Properties.Value!=null?
                    vGridControl1.Rows["rowWORK_CONTENT"].Properties.Value.ToString():"";
                string strOUTSOURCE_DATE = vGridControl1.Rows["rowOUTSOURCE_DATE"].Properties.Value!=null?
                    vGridControl1.Rows["rowOUTSOURCE_DATE"].Properties.Value.ToString():"";
                string strRETURN_DATE = vGridControl1.Rows["rowRETURN_DATE"].Properties.Value!=null?
                    vGridControl1.Rows["rowRETURN_DATE"].Properties.Value.ToString():"";
                string strMAINTAIN_COST = vGridControl1.Rows["rowMAINTAIN_COST"].Properties.Value != null ?
                    vGridControl1.Rows["rowMAINTAIN_COST"].Properties.Value.ToString().Length > 0 ?
                    vGridControl1.Rows["rowMAINTAIN_COST"].Properties.Value.ToString() : "NULL" : "NULL";
                string strISSUE_STATE = vGridControl1.Rows["rowISSUE_STATE"].Properties.Value!=null?
                    vGridControl1.Rows["rowISSUE_STATE"].Properties.Value.ToString():"";
                string strOUTSOURCE_STATE = vGridControl1.Rows["rowOUTSOURCE_STATE"].Properties.Value != null ?
                   vGridControl1.Rows["rowOUTSOURCE_STATE"].Properties.Value.ToString() : "";

                string strUPDATE_TIME = DateTime.Now.ToString();
                string strUPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;
                if (string.IsNullOrEmpty(strMAINTAIN_NO))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("维修编号不允许为空！", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (string.IsNullOrEmpty(strOUTSOURCE_STATE))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("外修时设备状态不允许为空！", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (string.IsNullOrEmpty(strISSUE_STATE))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("外修状态不允许为空！", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (IsEdit == "Add")
                {
                    string strOUTSOURCE_NO = "DOS" + DateTime.Now.ToString("yyyyMMddHHmmss");//委外编号                 
                    if (Common.CRUDHelper.Insert_MT_Outsource_Record_2(strOUTSOURCE_NO, strMAINTAIN_NO, strOUTSOURCE_FACTORY, strWORK_CONTENT,
                        strOUTSOURCE_DATE, strRETURN_DATE, strMAINTAIN_COST, strISSUE_STATE, strUPDATE_TIME, strUPDATE_USER, strOUTSOURCE_STATE))
                    {
                        setGridControlData();
                        XtraMessageBox.Show("委外维修记录新增成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                if (IsEdit == "Edit")
                {
                    string strOUTSOURCE_NO = vGridControl1.Rows["rowOUTSOURCE_NO"].Properties.Value.ToString();
                    if (Common.CRUDHelper.Update_MT_Outsource_Record(strOUTSOURCE_NO, strMAINTAIN_NO, strOUTSOURCE_FACTORY, strWORK_CONTENT,
                        strOUTSOURCE_DATE, strRETURN_DATE, strMAINTAIN_COST, strISSUE_STATE, strUPDATE_TIME, strUPDATE_USER, strOUTSOURCE_STATE))
                    {
                        setGridControlData();
                        XtraMessageBox.Show("委外维修记录修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                //每次保存后锁定信息输入
                vGridControl1.OptionsBehavior.Editable = false;
                for (int i = 0; i < vGridControl1.Rows.Count; i++)
                {
                    vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            ////不提供单击浏览操作，浏览与新建、修改共用操作
            //vGridControl1.DataSource = null;

            //MessageBox.Show("1、双击gridControl1，执行新增(建)、或修改外修单操作；" +
            //    "\r\n" + "2、清空并将当前行数据刷新至[右侧编辑区]中；" +
            //    "\r\n" + "3、如果[外修编号]为空，需自动产生，格式为“DOSyyyyMMddhhmmss。");
            SetVGridControl();

        }

        public override void DoAdd(Lamex.Interface.IButtonInfo sender)
        {
            //vGridControl1.DataSource = null;

            //MessageBox.Show("1、清空[右侧编辑区]，并自动产生[外修编号]；" 
            //    +"\r\n" + "2、加载需[委外维修清单]至[维修编号]下拉框中。"
            //    +"\r\n" + "3、当用户选择[维修编号]后，自动刷新[设备编号、设备名称]。");

            //SQL逻辑如下
            //SELECT MAINTAIN_NO, mr.MACHINE_NO,bi.MACHINE_DESC,PROCESS_PLAN,CONFIRM_PERSON
            // FROM[LamexMES_FAMA].[dbo].[MT_Maintain_Record] mr,
            //[LamexMESManager].[dbo].[FAMA_Account_Basic_Info] bi
            //WHERE mr.MACHINE_NO = bi.MACHINE_NO
            //AND PROCESS_PLAN = '委外维修' AND CONFIRM_PERSON IS NULL
            vGridControl1.OptionsBehavior.Editable = true;
            if (vGridControl1.Rows.Count > 0)
                for (int i = 0; i < vGridControl1.Rows.Count; i++)
                {
                    vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                }
            IsEdit = "Add";
            vGridControl1.FocusedRow = vGridControl1.Rows["rowMAINTAIN_NO"];
        }

        public override void DoEdit(Lamex.Interface.IButtonInfo sender)
        {
            SetVGridControl();
          
        }

        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            setGridControlData();
        }

        public override void DoSave(Lamex.Interface.IButtonInfo sender)
        {
            btnSave_Click(null,null);
        }

        private void SetVGridControl()
        {
            //不提供单击浏览操作，浏览、编辑共用操作
            vGridControl1.OptionsBehavior.Editable = true;
            IsEdit = "Edit";
            //获取当前行的数据
            int iRow = gridView1.FocusedRowHandle;
            string strOUTSOURCE_NO = gridView1.GetRowCellDisplayText(iRow, "OUTSOURCE_NO").ToString();//外修编号
            string strMAINTAIN_NO = gridView1.GetRowCellDisplayText(iRow, "MAINTAIN_NO").ToString();//维修编号
            string strMACHINE_NO = gridView1.GetRowCellDisplayText(iRow, "MACHINE_NO").ToString();//设备编号
            string strMACHINE_DESC = gridView1.GetRowCellDisplayText(iRow, "MACHINE_DESC").ToString();//设备描述
            string strOUTSOURCE_FACTORY = gridView1.GetRowCellDisplayText(iRow, "OUTSOURCE_FACTORY").ToString();//外修工厂
            string strWORK_CONTENT = gridView1.GetRowCellDisplayText(iRow, "WORK_CONTENT").ToString();//外修内容
            string strOUTSOURCE_DATE = gridView1.GetRowCellDisplayText(iRow, "OUTSOURCE_DATE").ToString();//外修日期
            string strRETURN_DATE = gridView1.GetRowCellDisplayText(iRow, "RETURN_DATE").ToString();//返厂日期
            string strMAINTAIN_COST = gridView1.GetRowCellDisplayText(iRow, "MAINTAIN_COST").ToString();//维修成本(RMB)
            string strISSUE_STATE = gridView1.GetRowCellDisplayText(iRow, "ISSUE_STATE").ToString();//外修状态
            string strOUTSOURCE_STATE = gridView1.GetRowCellDisplayText(iRow, "OUTSOURCE_STATE").ToString();//外修时设备状态
            string strUPDATE_TIME = gridView1.GetRowCellDisplayText(iRow, "UPDATE_TIME").ToString();//更新时间
            string strUPDATE_USER = gridView1.GetRowCellDisplayText(iRow, "UPDATE_USER").ToString();//更新用户


            //把数据显示到右边的VGridControl
            vGridControl1.Rows["rowOUTSOURCE_NO"].Properties.Value = strOUTSOURCE_NO;
            vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value = strMAINTAIN_NO;
            vGridControl1.Rows["rowMACHINE_NO"].Properties.Value = strMACHINE_NO;
            vGridControl1.Rows["rowMACHINE_DESC"].Properties.Value = strMACHINE_DESC;
            vGridControl1.Rows["rowOUTSOURCE_FACTORY"].Properties.Value = strOUTSOURCE_FACTORY;
            vGridControl1.Rows["rowWORK_CONTENT"].Properties.Value = strWORK_CONTENT;
            vGridControl1.Rows["rowOUTSOURCE_STATE"].Properties.Value = strOUTSOURCE_STATE;
            vGridControl1.Rows["rowOUTSOURCE_DATE"].Properties.Value = strOUTSOURCE_DATE;
            vGridControl1.Rows["rowRETURN_DATE"].Properties.Value = strRETURN_DATE;
            vGridControl1.Rows["rowMAINTAIN_COST"].Properties.Value = strMAINTAIN_COST;
            vGridControl1.Rows["rowISSUE_STATE"].Properties.Value = strISSUE_STATE;
            vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value = strUPDATE_TIME;
            vGridControl1.Rows["rowUPDATE_USER"].Properties.Value = strUPDATE_USER;

            vGridControl1.FocusedRow = vGridControl1.Rows["rowOUTSOURCE_FACTORY"];
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            DoView(null);
        }
    }
}